
sikap_df <- read_csv("./data/sikap_select.csv")

zip_codes <- 
  read_csv("./data/merged_post_codes.csv") %>%
  mutate(kab_code = paste0(str_sub(CC_4, 1,4))) %>%
  select(kode_pos, kab_code) %>%
  distinct() %>%
  group_by(kode_pos) %>%
  mutate(val = length(unique(kab_code))) %>%
  filter(val == 1)


#merge
merged_sikap_data <-
  left_join(sikap_df, zip_codes, by = c("demog_zipcode" = "kode_pos"))

vote_buy_data <-
  merged_sikap_data %>%
  mutate(prov_code = str_sub(kab_code, 1, 2)) %>%
  group_by(prov_code) %>%
  summarise(vb_val = mean(client_votebuy == 1, na.rm = T),
            elect_integrity_val = mean(elecint_event_integrity %in% c(3, 4), na.rm = T),
            trust_kpu_val = mean(trustinst_inst_kpu %in% c(3, 4), na.rm = T)
  ) %>%
  mutate(high_vb = vb_val > median(vb_val),
         low_integrity = elect_integrity_val < median(elect_integrity_val),
         low_kpu_trust = trust_kpu_val < median(trust_kpu_val))



estimation_data <- estimation_data %>%
  mutate(prov_code = as.character(prov_code)) %>%
  left_join(., vote_buy_data)



mod1 <- fixest::feols(avg_score_democ_support ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(high_vb == TRUE), cluster = ~ID)
mod2 <- fixest::feols(avg_score_trust_elect ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(high_vb == TRUE), cluster = ~ID)

mod3 <- fixest::feols(avg_score_democ_support ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(high_vb == FALSE), cluster = ~ID)
mod4 <- fixest::feols(avg_score_trust_elect ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(high_vb == FALSE), cluster = ~ID)


mods <- list(mod1, mod2, mod3, mod4)

table_df <-
  modelsummary(mods,
               stars = c("*" = 0.1, "**" = 0.05, "***" = 0.01),
               coef_map = c(
                 "pre_post" = "Post Election",
                 "validated_election_outcomeNot Elected" = "Outcome: Lost",
                 "pre_post:validated_election_outcomeNot Elected" = "Post x Lost"
               ),
               gof_map = c("nobs", "r.squared"),
               output = "data.frame"
  ) %>%
  mutate(term = ifelse(statistic == "std.error", "", term)) %>%
  select(-c(part, statistic)) %>%
  mutate_all(as.character)


# Append the new row to the table
latex_table <-
  table_df %>%
  kableExtra::kable("latex",
                    booktabs = T,
                    longtable = T,
                    escape = F,
                    cap = "\\label{tab:votebuy}Main Analysis, by Voter Perceptions of Vote Buying",
                    padding = 0,
                    col.names = linebreak(c("", "(1)", "(2)", "(3)", "(4)"), c("l", rep("c", 4))),
                    align = c("l", rep("c", 4)),
                    linesep = "") %>%
  kableExtra::row_spec(6, hline_after = T) %>%
  kableExtra::kable_styling(
    font_size = 9,
    latex_options = c("scale_down",
                      "hold_position",
                      "striped")) %>%
  
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "Supp. for Dem." = 1,
      "Trust in Elec." = 1,
      "Supp. for Dem." = 1,
      "Trust in Elec." = 1
    ),
    align = c("l", rep("c", 4))
  ) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "Vote Buying: High" = 2,
      "Vote Buying: Low" = 2
    ),
    align = c("l", rep("c", 4))
  )

cat("\\renewcommand{\\arraystretch}{0.9}\n", file = "./outputs/tables/table_a15.tex")
cat(latex_table, file = "./outputs/tables/table_a15.tex", append = TRUE)



